package im.zego.zimexample;

import android.app.Activity;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;


import im.zego.zim.callback.ZIMLoggedInCallback;
import im.zego.zim.entity.ZIMError;
import im.zego.zim.enums.ZIMErrorCode;
import im.zego.zimexample.ui.StartChatActivity;
import im.zego.zimexample.util.MyVideoView;

public class MainActivity extends MyBasicActivity {


    public EditText editText, editTokenText, editUserNameText;
    public MyVideoView myVideoView;



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //去掉窗口标题
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        //隐藏状态栏部分（电池、信号等）  全屏显示
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_example_login);
        myVideoView = findViewById(R.id.my_video_view);
        editText = findViewById(R.id.user_id);

        editUserNameText = findViewById(R.id.userName);
        // 初始化 SDK
        SDKManager.sharedInstance().create(this.getApplication());

       initVideoView();
    }

    private void initVideoView() {
        myVideoView.setVideoURI(Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.city));

        //播放
        myVideoView.start();
        //循环播放
        myVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                myVideoView.start();
                mediaPlayer.setOnInfoListener(new MediaPlayer.OnInfoListener() {
                    @Override
                    public boolean onInfo(MediaPlayer mp, int what, int extra) {
                        return false;
                    }
                });
            }
        });
    }

    public void login(View view) {
        String userID = editText.getText().toString();
        String userName = editUserNameText.getText().toString();

        // 登录
        SDKManager.sharedInstance().login(userID, userName, "", new ZIMLoggedInCallback() {
            @Override
            public void onLoggedIn(ZIMError zimError) {
                if (zimError.code != ZIMErrorCode.SUCCESS) {
                    Toast.makeText(MainActivity.this, "Login failed error info : " + zimError.code.name(), Toast.LENGTH_LONG).show();
                } else {
                    // 登录成功，进入下个页面
                    StartChatActivity.actionStart(MainActivity.this, userID, userName);
                }
            }
        });

    }

    @Override
    protected void onStart() {
        super.onStart();
        SDKManager.sharedInstance().showDebugLog(this);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        initVideoView();
    }

    @Override
    protected void onStop() {
        super.onStop();
        SDKManager.sharedInstance().closeDebugLog(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 销毁SDK
        SDKManager.sharedInstance().destroy();
    }

}
